<?php
require_once dirname(__FILE__) . '/../../../incls/php/dbutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/miscutils.class.php';
require_once dirname(__FILE__) . '/../../../incls/php/config.php';
$SIZE = 20;
session_name(SESSION);
session_start();
$_SESSION[TINYMCE_UPLOAD] = 'uploads/admin/';
$myPdo = DbUtils::createPdoInst();
$inst_id = MiscUtils::getParam('instid', 0);

$page = MiscUtils::getParam('pg', 1);
$cond_vals = new stdClass();
$cond_vals->c = 't.instance_id = :v1';
$cond_vals->v = array(':v1' => $inst_id);
$items = DbUtils::get($myPdo, TABLE_NAME_PREFIX_PLUGIN . basename(dirname(__FILE__)) . '_item', $cond_vals, 't.order_number', 'ASC', $page, $SIZE, NULL);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<style>
* {
  font-family: Trebuchet MS;
  font-size: 12px;
  padding: 0;
  margin: 0;
}
table.list {
  border-left: 1px solid #ddd;
  border-top: 1px solid #ddd;
  font-size: 12px;
  width: 100%;
}
table.list tr.none td {
  padding: 0 5px;
  background-color: #fff;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  word-wrap: break-word;
}
table.list td.rowodd {
  padding: 0 5px;
  background-color: #eee;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  word-wrap: break-word;
  vertical-align: top;
  padding-top: 2px;
}
table.list td.roweven {
  padding: 0 5px;
  background-color: #fff;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  word-wrap: break-word;
  vertical-align: top;
  padding-top: 2px;
}
table.list td.rowodd:hover {
  background-color: #f4f3b2;
}
table.list td.roweven:hover {
  background-color: #f4f3b2;
}
table.form {
  border: 1px solid #ddd;
}
table.form td {
  background-color: #fff;
  vertical-align: top;
  border: 1px solid #ddd;
}
table.form td.title {
  background-color: #f1d0d0;
  border-top: 1px solid #fff;
}
table.form td.content textarea {
  margin: 0;
  padding: 0;
}
table.form td.content input {
  width: 95%;
  height: 25px;
  border: 0;
  padding: 2px;
}
</style>
</head>
<body>
<button onclick="add();">Add new</button>
<button onclick="update();">Update</button>
<table id="items" class="list"></table>
<div id="pagination"></div>
<button onclick="add();">Add new</button>
<button onclick="update();">Update</button>
<script type="text/javascript" src="../../../libs/jquery/jquery-1.6.2.min.js"></script>
<script type="text/javascript" src="../../../incls/js/myutils.js"></script>
<script type="text/javascript" src="../../../libs/tiny_mce/tiny_mce.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/swfupload.queue.js"></script>
<script type="text/javascript" src="../../../libs/swfupload/myupload.js"></script>
<script type="text/javascript" src="../../../incls/js/pagination.js"></script>

<script type="text/javascript">
  var _items = JSON.parse('<?php echo addslashes(json_encode($items)); ?>');
  $(function(){
    var elm = document.getElementById('pagination');
    new ModulePagination(document, elm, null, null, null, null, {
      page: _items.p,
      total: _items.t,
      limit: _items.s,
      callbackFunc: function(page) {
        location.href = 'admin.php?instid=4&pg=' + page;
      },
      contenxt: this
    });
    
    var table, tr, td;
    table = document.getElementById('items');
    MyUtils.removeTableRows(table, 0);
    for (var idx in _items.d) {
      var obj = _items.d[idx];
      tr = table.insertRow(-1);
      td = tr.insertCell(-1);
      td.className = (idx % 2 == 0) ? 'roweven' : 'rowodd';
      var itable, itr, itd, ielm;
      itable = document.createElement('table');
      td.appendChild(itable);
      itable.className = 'form';
      itr = itable.insertRow(-1);
      itd = itr.insertCell(-1);
      itd.className = 'title';
      itd.appendChild(document.createTextNode('#' + (parseInt(idx, 10) + 1 + (_items.p - 1) * _items.s)));
      ielm = document.createElement('input');
      ielm.type = 'submit';
      ielm.value = 'Delete';
      ielm._obj = obj;
      ielm.onclick = function() {
        remove(this._obj.id);
      }
      itd.appendChild(ielm);
      itd = itr.insertCell(-1);
      itd.className = 'content';
      ielm = document.createElement('input');
      ielm.type = 'text';
      ielm.value = obj.order_number;
      ielm._obj = obj;
      ielm.onchange = function() {
        this._obj.order_number = this.value;
      }
      itd.appendChild(ielm);
      itd = itr.insertCell(-1);
      itd.className = 'title';
      itd.appendChild(document.createTextNode('Title'));
      itd = itr.insertCell(-1);
      itd.className = 'content';
      ielm = document.createElement('input');
      ielm.type = 'text';
      ielm.value = obj.title;
      ielm._obj = obj;
      ielm.onchange = function() {
        this._obj.title = this.value;
      }
      itd.appendChild(ielm);
      itd = itr.insertCell(-1);
      itd.style.width = '220px';
      itd.className = 'title';
      ielm = document.createElement('span');
      ielm.id = 'up' + obj.id;
      itd.appendChild(ielm);
      ielm = document.createElement('span');
      ielm.id = 'can' + obj.id;
      ielm.style.display = 'none';
      itd.appendChild(ielm);
      itr = itable.insertRow(-1);
      itd = itr.insertCell(-1);
      itd.className = 'title';
      itd.appendChild(document.createTextNode('Description'));
      itd = itr.insertCell(-1);
      itd.colSpan = 3;
      itd.className = 'content';
      ielm = document.createElement('textarea');
      ielm.id = 'desc' + obj.id;
      ielm.value = obj.description;
      ielm._obj = obj;
      itd.appendChild(ielm);
      itd = itr.insertCell(-1);
      itd.className = 'content';
      itd.id = 'img' + obj.id;
      elm = document.createElement('img');
      elm.src = obj.path;
      elm.style.width = '200px';
      itd.appendChild(elm);

      new MyUpload('up' + obj.id, 'upload.php', function(msgs) {
        var obj, elm, div;
        obj = JSON.parse(msgs);
        div = document.getElementById('img' + obj.id);
        MyUtils.removeChildElements(div);
        elm = document.createElement('img');
        elm.src = obj.path;
        elm.style.width = '200px';
        div.appendChild(elm);
      }, {
        dir: '../../../',
        button_image_url: 'images/browse.png',
        button_width: 94,
        button_height: 25,
        file_upload_limit: 1,
        file_queue_limit: 1,
        file_size_limit: '8MB',
        file_types: '*.jpg;*.jpeg;*.png;*.gif',
        file_types_description: 'Image Files',
        cancelBtnId: 'can' + obj.id,
        post_params: {
          id: obj.id
        },
        debug: 0
      });
      tinyMCE.init( {
        // General options
        mode: 'exact',
        elements: 'desc' + obj.id,
        theme: 'advanced',

        plugins: 'pdw,filebrowser,autolink,lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,wordcount,advlist',
        // Theme options
        theme_advanced_buttons1: 'pdw_toggle,|,undo,redo,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,fontselect,fontsizeselect,|,forecolor,backcolor,|,image,media,|,preview',
        theme_advanced_buttons2: 'search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,link,unlink,anchor,cleanup,help,code,|,insertdate,inserttime',
        theme_advanced_buttons3: 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,advhr,|,print,|,ltr,rtl,|,fullscreen',
        theme_advanced_buttons4: 'insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak',
        theme_advanced_toolbar_location: 'top',
        theme_advanced_toolbar_align: 'left',
        theme_advanced_statusbar_location: 'bottom',
        theme_advanced_resizing: false,

        file_browser_callback: 'filebrowser',
        pdw_toggle_on: '1',
        pdw_toggle_toolbars: '2,3,4',
        // Example content CSS (should be your site CSS)
        theme_advanced_font_sizes: '10px,11px,12px,13px,14px,15px,16px,17px,18px,19px,20px,21px,22px,23px,24px,25px,26px,27px,28px,29px,30px',
        theme_advanced_fonts : "黑体=黑体;"+
                               "宋体=宋体;"+
                               "Andale Mono=andale mono,times;"+
                               "Arial=arial,helvetica,sans-serif;"+
                               "Arial Black=arial black,avant garde;"+
                               "Book Antiqua=book antiqua,palatino;"+
                               "Comic Sans MS=comic sans ms,sans-serif;"+
                               "Courier New=courier new,courier;"+
                               "Georgia=georgia,palatino;"+
                               "Helvetica=helvetica;"+
                               "Impact=impact,chicago;"+
                               "Symbol=symbol;"+
                               "Tahoma=tahoma,arial,helvetica,sans-serif;"+
                               "Terminal=terminal,monaco;"+
                               "Times New Roman=times new roman,times;"+
                               "Trebuchet MS=trebuchet ms,geneva;"+
                               "Verdana=verdana,geneva;"+
                               "Webdings=webdings;"+
                               "Wingdings=wingdings,zapf dingbats"
      });
    }
    if (_items.d.length == 0) {
      tr = table.insertRow(-1);
      tr.className = 'none';
      td = tr.insertCell(-1);
      td.appendChild(document.createTextNode('No Results Found'));
    }
    parent.update_height();
  });

  function add() {
    var obj = {
      instance_id: <?php echo $inst_id; ?>,
      order_number: 0,
      title: 'Untitled',
      description: '',
      size: 0,
      path: ''
    };
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=add&q=' + encodeURIComponent(JSON.stringify(obj)),
      success: function(msg){
        try {
          location.reload();
        } catch (ex) {
        }
      }
    });
  }

  function update() {
    var items = [];
    for (var idx in _items.d) {
      var obj = _items.d[idx];
      items[items.length] = {
        id: obj.id,
        order_number: obj.order_number,
        title: obj.title,
        description: tinyMCE.get('desc' + obj.id).getContent()
      };
    }
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=update&q=' + encodeURIComponent(JSON.stringify(items)),
      success: function(msg){
        try {
          location.reload();
        } catch (ex) {
        }
      }
    });
  }

  function remove(id) {
    $.ajax({
      type: 'POST',
      url: '<?php echo basename(__FILE__, '.php'); ?>.server.php',
      data: 'action=remove&q=' + encodeURIComponent(id),
      success: function(msg){
        try {
          location.reload();
        } catch (ex) {
        }
      }
    });
  }
</script>
</body>
</html>
